package msu.erdc.hci;

import msu.erdc.hci.customObjects.ContactObject;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class EditContactDetail extends Fragment {

	Button cancelButton;
	EditText editlname;
	EditText editfname;
	EditText editoffice;
	EditText editmobile;
	EditText editemail;
	EditText categoryText;
	EditText editaddress1;
	EditText editaddress2;
	int contactIndex;
	ContactObject clickedContact;
	boolean canceled;
//	public static String editedname;
//	public static String editedofficephone;
//	public static String editedmobilephone;
//	public static String editedemailaddress;
//	public static String editedaddress1;
//	public static String editedaddress2;
//	public static String editedcategory;
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		
		View view =  inflater.inflate(R.layout.contact_editeddetail, container, false);
		
		cancelButton = (Button) view.findViewById(R.id.cancelButton);
		
		editlname = (EditText) view.findViewById(R.id.lnameText);
		editfname = (EditText) view.findViewById(R.id.nameText);
		editoffice = (EditText) view.findViewById(R.id.officephoneText);
		editmobile = (EditText) view.findViewById(R.id.cellphoneText);
		editemail = (EditText) view.findViewById(R.id.emailText);
		editaddress1 = (EditText) view.findViewById(R.id.addressText);
		editaddress2 = (EditText) view.findViewById(R.id.address2Text);
		categoryText = (EditText) view.findViewById(R.id.categoryText);
		 
		editlname.setText(clickedContact.lastname);
		editfname.setText(clickedContact.firstname);
		editmobile.setText(clickedContact.mobile);
		editoffice.setText(clickedContact.phone);
		editemail.setText(clickedContact.emailaddress);
		editaddress1.setText(clickedContact.street);
		editaddress2.setText(clickedContact.cityState);
		categoryText.setText(clickedContact.category);
		
		cancelButton.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
//					clickedContact.firstname=editfname.getText().toString();
//					clickedContact.lastname = editlname.getText().toString();
//					clickedContact.phone = editoffice.getText().toString();
//					clickedContact.mobile = editmobile.getText().toString();
//					clickedContact.emailaddress = editemail.getText().toString();
//					clickedContact.street = editaddress1.getText().toString();
//					clickedContact.cityState = editaddress2.getText().toString();
//					clickedContact.category = categoryText.getText().toString();
//					
//					ContactDetail contactFrag = new ContactDetail();
//					Bundle arguments = new Bundle();
//					arguments.putInt("index", contactIndex);
//					contactFrag.setArguments(arguments);
//					FragmentTransaction ft = getFragmentManager().beginTransaction();
//					ft.replace(android.R.id.content, contactFrag, "save");
//					ft.addToBackStack(null);
//					ft.commit();
					canceled = true;
					FragmentManager man = getActivity().getFragmentManager();
					man.popBackStackImmediate();
				}});

		return view;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		contactIndex = 0;
		Bundle arguments = getArguments();
		if(arguments!=null)
		{
			contactIndex = arguments.getInt("index");
		}
		clickedContact = ((Global)getActivity().getApplication()).contacts.get(contactIndex);
		canceled = false;
	}
	
	@Override
	public void onDestroy()
	{
		if(!canceled)
		{
			clickedContact.firstname=editfname.getText().toString();
			clickedContact.lastname = editlname.getText().toString();
			clickedContact.phone = editoffice.getText().toString();
			clickedContact.mobile = editmobile.getText().toString();
			clickedContact.emailaddress = editemail.getText().toString();
			clickedContact.street = editaddress1.getText().toString();
			clickedContact.cityState = editaddress2.getText().toString();
			clickedContact.category = categoryText.getText().toString();
		}
		super.onDestroy();
	}
	
	
	
}